import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel, QGridLayout, QHBoxLayout, QTextEdit
from PyQt5.QtGui import QColor, QPalette
from PyQt5.QtCore import Qt

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel
from PyQt5.QtGui import QColor, QPalette
from PyQt5.QtCore import Qt

class InitialColorDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.current_color = QColor(255, 0, 0)  # 初始为红色
        self.init_ui()
        
    def init_ui(self):
        layout = QVBoxLayout()
        
        self.color_display = QLabel()
        self.color_display.setMinimumHeight(100)
        self.color_display.setStyleSheet(f"background-color: {self.current_color.name()}; border: 2px solid black;")
        
        self.info_label = QLabel(f'当前颜色: {self.current_color.name()}')
        
        select_btn = QPushButton('选择新颜色')
        select_btn.clicked.connect(self.select_with_initial)
        
        layout.addWidget(self.color_display)
        layout.addWidget(self.info_label)
        layout.addWidget(select_btn)
        self.setLayout(layout)
        self.setWindowTitle('带初始颜色的对话框')
        
    def select_with_initial(self):
        from PyQt5.QtWidgets import QColorDialog
        
        # 使用当前颜色作为初始颜色
        color = QColorDialog.getColor(
            initial=self.current_color,
            parent=self,
            title="选择新颜色"
        )
        
        if color.isValid():
            self.current_color = color
            self.color_display.setStyleSheet(f"background-color: {color.name()}; border: 2px solid black;")
            self.info_label.setText(f'当前颜色: {color.name()}\nRGB: ({color.red()}, {color.green()}, {color.blue()})')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = InitialColorDemo()
    window.show()
    sys.exit(app.exec_())
